package com.sys.ai.config;

import com.sys.ai.domain.AiEmbeddingDb;
import com.sys.common.exception.ServiceException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

/**
 * @author LGY
 * Create by 2025/5/20 11:07
 */
@Slf4j
@Data
public class PostgreClient {

    private String url;
    private String username;
    private String password;

    public static PostgreClient getInstance(AiEmbeddingDb aiEmbeddingDb) {
        return new PostgreClient(aiEmbeddingDb.getUrl(), aiEmbeddingDb.getUsername(), aiEmbeddingDb.getPassword());
    }

    private PostgreClient(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public Connection connection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(this.url, this.username, this.password);
        } catch (Exception e) {
            log.error("Postgre数据库连接异常", e);
            throw new ServiceException("Postgre数据库连接异常");
        }
        return conn;
    }

    public void executeUpdate(String sql, List<Object> params) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            conn = connection();
            preparedStatement = conn.prepareStatement(sql);
            if (!CollectionUtils.isEmpty(params)) {
                for (int i = 0; i < params.size(); i++) {
                    Object param = params.get(i);
                    if (param instanceof String) {
                        preparedStatement.setString(i + 1, (String) param);
                    } else {
                        preparedStatement.setObject(i + 1, param);
                    }
                }
            }
            preparedStatement.executeUpdate();
            preparedStatement.close();
        } catch (Exception e) {
            log.error("Postgre数据库update异常", e);
            throw new ServiceException("Postgre数据库update异常");
        } finally {
            close(conn, preparedStatement);
        }
    }

    public ResultSet executeQuery(String sql, List<Object> params) {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            conn = connection();
            preparedStatement = conn.prepareStatement(sql);
            if (!CollectionUtils.isEmpty(params)) {
                for (int i = 0; i < params.size(); i++) {
                    Object param = params.get(i);
                    if (param instanceof String) {
                        preparedStatement.setString(i + 1, (String) param);
                    } else {
                        preparedStatement.setObject(i + 1, param);
                    }
                }
            }
            rs = preparedStatement.executeQuery();
        } catch (Exception e) {
            log.error("Postgre数据库query异常", e);
            throw new ServiceException("Postgre数据库query异常");
        }
        return rs;
    }

    public void close(Connection conn, PreparedStatement preparedStatement) {
        try {
            if (conn != null) {
                conn.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (Exception e) {
            log.error("Postgre数据库关闭异常", e);
        }
    }
}
